import {createRouter, createWebHashHistory} from 'vue-router'
import ChooseIdentity from '../views/ChooseIdentity'
import Login from '../views/Login'
import LayOut from '../views/LayOut'
import AddStudent from '../components/AddStudent'
import AddGrade from '../components/AddGrade'
import EnrollmentCard from '../components/EnrollmentCard'
import QueryGrade from '../components/QueryGrade'
import ShowStudent from '../components/ShowStudent'
import AddSchool from '../components/superManager/AddSchool'
import AddEnterprise from '../components/superManager/AddEnterprise'
import Verify from '../components/Verify'
import Schools from '../components/superManager/Schools'
import Enterprises from '../components/superManager/Enterprises'

import store from '../store'
import ShowGradeList from '../components/ShowGradeList'

const routes = [
    {
        path: '/chooseIdentity',
        name: 'chooseIdentity',
        component: ChooseIdentity
    },
    {
        path: '/login',
        name: 'login',
        component: Login
    },
    {
        path: '/',
        name: 'LayOut',
        component: LayOut,
        props: true,
        children: [
            {
                path: '/addStudent',
                name: 'addStudent',
                component: AddStudent
            },
            {
                path: '/addGrade',
                name: 'addGrade',
                component: AddGrade
            },
            {
                path: '/enrollment',
                name: 'enrollment',
                component: EnrollmentCard,
                props: true
            },
            {
                path: '/showStudent',
                name: 'showStudent',
                component: ShowStudent
            },
            {
                path: '/gradeList',
                name: 'gradeList',
                component: ShowGradeList,
                props: true
            },
            {
                path: '/queryGrade',
                name: 'queryGrade',
                component: QueryGrade
            },
            {
                path: '/verify/:type*',
                name: 'verify',
                component: Verify,
                props: true
            },
            {
                path: '/superManager/addSchool',
                name: 'addSchool',
                component: AddSchool
            },
            {
                path: '/superManager/addEnterprise',
                name: 'addEnterprise',
                component: AddEnterprise
            },
            {
                path: '/superManager/Schools',
                name: 'schools',
                component: Schools
            },
            {
                path: '/superManager/Enterprises',
                name: 'enterprises',
                component: Enterprises
            }
        ]
    }
]

const router = createRouter({
    history: createWebHashHistory(),
    routes,
})

router.beforeEach(async(to, from, next) => {
    if(store.state.authToken || to.path === '/login') {
        next()
    } else {
        if(to.path === '/chooseIdentity') {
            next()
        } else {
            next({ name: 'chooseIdentity'})
        }
    }
})

export default router